﻿//Author:Michal Sporna
//License: https://operationsneakpeek.codeplex.com/license

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Util.Classes
{
    /// <summary>
    /// represents item that is in specific map cell
    /// with specific coordinates
    /// </summary>
    public class ItemToDraw
    {
        #region properties
        public string ItemID; //name/ID of the item to draw
        public Vector2 Position;
        public bool Collected;
        public int Count;//how many items added to the inventory when this is collected
        #endregion

        #region public fields
        //helpers
        public string PosXString;
        public string PosYString;
        //dimensions
        public int[] Dimensions;
        public bool DimensionsSet;

       

        #region TEXTURE
        public bool TextureSet;
        public Texture2D TextureInstance;
        public string TextureName;
        #endregion

        #endregion

        #region methods

        /// <summary>
        /// constructor
        /// </summary>
        public ItemToDraw()
        {
            this.Collected = false;
            Count = 1;
            
        }

        /// <summary>
        /// check if avatar is getting the item
        /// </summary>
        public void IsAvatarGettingThisItem()
        {
           //declare
            Item i = null;

            try
            {
                if (this.Collected)
                {
                    //if already collected do not add again
                    return;
                }

                if (Cache.Avatar != null)
                {

                    //see if those 2 rectangles intersect:
                    if (World.WorldManager.IsObstacleTouched(new int[]{(int)this.Position.X,(int)this.Position.Y,Dimensions[0],Dimensions[1]},new int[]{(int)Cache.Avatar.CurrentPosition.X, (int)Cache.Avatar.CurrentPosition.Y, Cache.Avatar.Width, Cache.Avatar.Height}))
                    {
                        //do not draw already collected item anymore
                        //remove from items to draw
                        //set collected to true to make it happen
                        this.Collected = true;

                        //get item instance
                        i = Levels.LevelManager.GetItemByName(this.ItemID);
                    
                        //raise event
                        if (i != null)
                        {
                            for (int ct = 0; ct < i.Event.Count; ct++)
                            {
                                if (i.Event[ct].EventType == Helpers.PublicEnums.EventType.onItemCollected)
                                {
                                    i.Event[ct].Parameters[0].Parameter = Count;//1 item collected
                                    i.Event[ct].Parameters[1].Parameter = i;//what item collected


                                    //raise
                                    World.EventHandler.RaiseEvent(i.Event[ct]);

                                    break;
                                }
                            }
                        }
                       
                    }
                }

                

            }
            catch (Exception ex)
            {
                Util.ErrorLogging.LogError(ex);
                return;
            }
        }



        #endregion

    }
}
